package v1

import (
	"auth/bll"
	"auth/model"
	"auth/server/web/middleware"
	"auth/utils"
	"github.com/gin-gonic/gin"
)

var Dept = &dept{}

type dept struct{}

func (a *dept) Init(r *gin.RouterGroup) {
	g := r.Group("/dept", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
		g.POST("/getDeptList", a.getDeptList)
		g.POST("/exclude", a.getDeptExclude)
	}
}

func (a *dept) create(c *gin.Context) {
	var (
		in  = &model.DeptCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Dept.Create(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *dept) update(c *gin.Context) {
	var (
		in  = &model.DeptUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Dept.Update(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *dept) list(c *gin.Context) {
	var (
		in  = &model.DeptListRequest{}
		out = &model.DeptListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.Dept.List(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}

func (a *dept) delete(c *gin.Context) {
	var (
		in  = &model.DeptDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Dept.Delete(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

// getDeptList	分页获取基础列表
func (a *dept) getDeptList(c *gin.Context) {
	var (
		in  = &model.GetDeptListRequest{}
		out = &model.GetDeptListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if out, err = bll.Dept.GetDeptList(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}

// getDeptExclude	修改部门树排除本身
func (a *dept) getDeptExclude(c *gin.Context) {
	var (
		in  = &model.GetDeptExcludeRequest{}
		out = &model.GetDeptExcludeResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if out, err = bll.Dept.GetDeptExclude(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}
